package com.example.mybatis.utils.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.SaTokenException;
import com.example.mybatis.utils.ResultUtil;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import org.springframework.web.util.NestedServletException;

import javax.servlet.ServletException;

/**
 * ????????????????????????????
 * @author lenovo
 *
 */
@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandle {
	
	/**
	 * ??????
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(value=Exception.class)
	public ResultUtil exception(Exception exception){
		exception.printStackTrace();
		return ResultUtil.error("服务器内容错误");
	}
	
	/**
	 * ??????????
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(value=ArithmeticException.class)
	public ResultUtil exception(ArithmeticException exception){
		exception.printStackTrace();
		return ResultUtil.error("算术异常");
	}
	
	/**
	 * ?????????java?????
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(value=MyException.class)
	public ResultUtil exception(MyException exception){
		exception.printStackTrace();
		return ResultUtil.error(exception.getErrCode(),exception.getErrMsg());
	}

	@ExceptionHandler(NotLoginException.class)
	public ResultUtil handlerNotLoginException(NotLoginException exception){
		exception.printStackTrace();
		return ResultUtil.error("F401","未登录");
	}


	@ExceptionHandler(NotPermissionException.class)
	public ResultUtil handlerNotLoginException(NotPermissionException exception){
		exception.printStackTrace();
		return ResultUtil.error("F402","无此功能权限");
	}
}
